﻿/*日志显示与记录类，支持日志文件最大限制，支持把日志同步显示到RichTextBox里
调用方法：
MyLog.Instance.SetLogWin(this.rtb_message);//设置日志显示窗口（选做，且只设置一次即可）
MyLog.Instance.log("hello"); //写日志，同时显示日志（如果执行过SetLogWin）
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using CommonTools;

namespace NS_Log
{
    /// <summary>
    /// 日志核心类
    /// </summary>
    public class MyLog
    {
        //public static string logPath = Application.StartupPath +"\\AnyChatDemoLog.log";
        private string logPath = cs_common.getMyDocumentDir() + "\\ControllerServer.log";
        private string oldPath = cs_common.getMyDocumentDir() + "\\ControllerServer.old";
        private int LOG_MAX_LENGTH = 5*1024*1024;
        private static MyLog s_Instance;

        private System.Windows.Forms.RichTextBox rtb_message;
        public MyLog(System.Windows.Forms.RichTextBox rtb_message = null)
        {
            this.rtb_message = rtb_message;
        }
        /// <summary>
        /// 设置日志显示窗口
        /// </summary>
        /// <param name="rtb_message"></param>
        public void SetLogWin(System.Windows.Forms.RichTextBox rtb_message)
        {
            this.rtb_message = rtb_message;
        }

        public static MyLog Instance
        {
            get
            {
                if (s_Instance == null)
                    s_Instance = new MyLog();

                return s_Instance;
            }

            set
            {
                if (s_Instance != null)
                    return;

                s_Instance = value;
            }
        }

        /// <summary>
        /// 生成log
        /// </summary>
        /// <param name="log"></param>
        private void WriteLog(string log)
        {
            try
            {
                File.AppendAllText(logPath, "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + log + "\r\n");
                FileInfo fi = new FileInfo(logPath);
                if(fi.Length>LOG_MAX_LENGTH)
                {    
                    File.Delete(oldPath);
                    File.Move(logPath, oldPath);
                }
            }
            catch (Exception)
            {
            }
        }

        private delegate void LogDelegate(string text);
        /// <summary>
        /// 写日志，同时显示到日志窗口里
        /// </summary>
        /// <param name="msg">日志内容</param>
        public void log(string msg)
        {
            if (rtb_message != null && rtb_message.InvokeRequired)
            {
                rtb_message.BeginInvoke(new LogDelegate(log), new object[] { msg });
            }
            else
            {
                WriteLog(msg);
                if (rtb_message != null)
                {
                    if (rtb_message.Lines.Length > 500)
                    {
                        rtb_message.Text = rtb_message.Text.Substring(rtb_message.Text.Length / 2);
                    }
                    rtb_message.AppendText(DateTime.Now.ToString("HH:mm:ss") + " " + msg + "\n");
                    rtb_message.SelectionStart = rtb_message.Text.Length;
                    rtb_message.ScrollToCaret();
                }
            }
        }
    }
}
